﻿/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: Swap / Pagefile
* Version: 0.1
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["swap"] = new Object();
myPlugins["swap"]["name"] = getLocalizedString2("mod_sysm_plugin_swap_name");
myPlugins["swap"]["onlyonce"] = true;
myPlugins["swap"]["newObject"] = function (NewSettings) { return new plugin_swap_sensor(NewSettings); }

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_swap_sensor(NewSettings) {
	// Required variables
	this.plugin = "swap";
	this.settings = NewSettings;
	this.edit = plugin_swap_edit;
	this.ReDraw = plugin_swap_ReDraw;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;


	//Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.barcolor) this.settings.barcolor = "#008000";
	if(!this.settings.updspeed) this.settings.updspeed = 20;


	//Get WMI
	if(system.platform=="windows") {
		if(VBSEnabled) this.PageFiles = runCommand('cscript.exe //nologo "' + mod_sysmonitor_path_swap + '"').split("\n");
		else this.PageFiles = runCommand('"' + mod_sysmonitor_WMIExecQuery + '" "Name" "1" "SELECT * FROM Win32_PageFileSetting"').split("\n");
		for(var i = 0 ; i < this.PageFiles.length ; i++ )
			this.PageFiles[i] = this.PageFiles[i].replace("\\","\\\\");
		if((this.settings.text==3)&&(this.PageFiles.length ==0)) this.settings.text=0;
	}


	//The Sonsore
	this.swap = new class_PercentBar(this.frame,0,0,getLocalizedString2("mod_sysm_plugin_swap_label"));
	this.swap.PercentColor(this.settings.color);
	this.swap.PercentBarColor(this.settings.barcolor);


	// Update timer
	var obj = this;
	this.timer = new Timer();
	this.timer.interval = this.settings.updspeed;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_swap_update.call(obj); };
	plugin_swap_update.call(obj);


	// Calculating sensore height (required!)
	this.frame.height = this.swap.background.vOffset + this.swap.background.height;
	return true;

// --------------------------------------------------------------------------------
// Helper Functions

	function plugin_swap_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_barcolor");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor;
	
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_swap_pref_text");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		if((system.platform=="windows") && (this.PageFiles.length>0)) {
			Drive_Edit_DialogFields[FieldID].option = new Array(getLocalizedString2("mod_sysm_plugin_swap_pref_free"),getLocalizedString2("mod_sysm_plugin_swap_pref_used"),getLocalizedString2("mod_sysm_plugin_swap_pref_total"),getLocalizedString2("mod_sysm_plugin_swap_pref_ondisk"));
			Drive_Edit_DialogFields[FieldID].optionValue = new Array(0,1,2,3);
		} else {
			Drive_Edit_DialogFields[FieldID].option = new Array(getLocalizedString2("mod_sysm_plugin_swap_pref_free"),getLocalizedString2("mod_sysm_plugin_swap_pref_used"),getLocalizedString2("mod_sysm_plugin_swap_pref_total"));
			Drive_Edit_DialogFields[FieldID].optionValue = new Array(0,1,2);
		}
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.text;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_updatespeed_label");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(1,2,3,5,10,15,20,25,30);
		Drive_Edit_DialogFields[FieldID].option = new Array();
		for(var i = 0; i < Drive_Edit_DialogFields[FieldID].optionValue.length ; i++ )
			Drive_Edit_DialogFields[FieldID].option[i] = getLocalizedString2("mod_sysm_plugin_updatespeed_" + Drive_Edit_DialogFields[FieldID].optionValue[i]);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.updspeed;


		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.swap.PercentColor(this.settings.color);
	
			this.settings.barcolor = Drive_Edit_DialogResults[1];
			this.swap.PercentBarColor(this.settings.barcolor);
	
			this.settings.text = Drive_Edit_DialogResults[2];

			if ( this.settings.updspeed != Drive_Edit_DialogResults[3] ) {
				this.settings.updspeed = Drive_Edit_DialogResults[3];
				this.timer.interval = this.settings.updspeed;
			}

			mod_sysmonitor_ReDraw();
			plugin_swap_update.call(this);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}

	function plugin_swap_update() {
		if((system.platform=="windows") && (this.PageFiles.length>0)) {
			var sm = 0;
			var sa = 0;
			var sc = 0;
			for(var i = 0 ; i < this.PageFiles.length ; i++ ) {
				sm += Swap_MaximumSize(this.PageFiles[i]);
				sa += Swap_AllocatedBaseSize(this.PageFiles[i]);
				sc += Swap_CurrentUsage(this.PageFiles[i]);
			}
			if(this.settings.text==1) // Used
				this.swap.PercentBarValue( 100 * sc / sm ,bytesToUIString(sc * 1048576));
			else if(this.settings.text==2) // Total
				this.swap.PercentBarValue( 100 * sc / sm ,bytesToUIString(sm * 1048576));
			else if(this.settings.text==3) // Allocated on Disk
				this.swap.PercentBarValue( 100 * sc / sm ,bytesToUIString(sa * 1048576));
			else // Free
				this.swap.PercentBarValue( 100 * sc / sm ,bytesToUIString((sm-sc) * 1048576));
			this.swap.PercentBarPeak( 100 * sa / sm );
		} else if(system.memory.totalVirtual > 0)
			if(this.settings.text==1)
				this.swap.PercentBarValue( 100*(system.memory.totalVirtual - system.memory.availVirtual)/system.memory.totalVirtual ,bytesToUIString( system.memory.totalVirtual - system.memory.availVirtual ));
			else if(this.settings.text==2)
				this.swap.PercentBarValue( 100*(system.memory.totalVirtual - system.memory.availVirtual)/system.memory.totalVirtual ,bytesToUIString( system.memory.totalVirtual ));
			else
				this.swap.PercentBarValue( 100*(system.memory.totalVirtual - system.memory.availVirtual)/system.memory.totalVirtual ,bytesToUIString( system.memory.availVirtual ));
		else
			this.swap.PercentBarValue(0,"*disabled*");
	
		return true;
	}

	function Swap_MaximumSize(DeviceName) {
		try {
			return wmi.Get( 'Win32_PageFileSetting.Name="' + DeviceName + '"' ).Properties_.Item( "MaximumSize" ).Value;
		} catch (e) { print("WMI Error (Swap_MaximumSize of " + DeviceName + "): " + e); return 0; }
	}

	function Swap_AllocatedBaseSize(DeviceName) {
		try {
			return wmi.Get( 'Win32_PageFileUsage.Name="' + DeviceName + '"' ).Properties_.Item( "AllocatedBaseSize" ).Value;
		} catch (e) { print("WMI Error (Swap_AllocatedBaseSize of " + DeviceName + "): " + e); return 0; }
	}

	function Swap_CurrentUsage(DeviceName) {
		try {
			return wmi.Get( 'Win32_PageFileUsage.Name="' + DeviceName + '"' ).Properties_.Item( "CurrentUsage" ).Value;
		} catch (e) { print("WMI Error (Swap_CurrentUsage of " + DeviceName + "): " + e); return 0; }
	}

	function Swap_PeakUsage(DeviceName) {
		try {
			return wmi.Get( 'Win32_PageFileUsage.Name="' + DeviceName + '"' ).Properties_.Item( "PeakUsage" ).Value;
		} catch (e) { print("WMI Error (Swap_PeakUsage of " + DeviceName + "): " + e); return 0; }
	}

	function plugin_swap_ReDraw() {
		this.frame.width = mod_sysmonitor_width;
		this.swap.style = preferences.mod_sysmonitor_bar_style.value;
		this.swap.size = mod_sysmonitor_size;
		this.frame.height = this.swap.background.vOffset + this.swap.background.height;
		return true;
	}

}

// --------------------------------------------------------------------------------
// The END